package gui.sidebar.relations;

import exceptions.RelationAlreadyInSetException;
import exceptions.RelationNotInSetException;
import queped.quepedconstants.Strings;

import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: Jason Ye
 * Date: 31/01/2012
 * Time: 18:26
 */
public class RelationContainerPanel extends JPanel {

    //scrollable list
    private ScrollableRelationList scrollableRelationList = new ScrollableRelationList();

    // collection panel for action buttons
    private JPanel buttonsPanel;

    // action buttons
    private JButton removeButton = new JButton("Remove");
    private JButton addButton = new JButton("Add");
    private JButton renameButton = new JButton("Rename");

    public RelationContainerPanel() {

        this.setSize(new Dimension(400, 400));

        // titled border
        this.setBorder(BorderFactory.createTitledBorder(Strings.RELATION));
         //set layout to border layout
        this.setLayout(new BorderLayout());

        // display existing relations
        this.add(scrollableRelationList, BorderLayout.CENTER);

        // create buttons panel
        buttonsPanel = new JPanel();

        // display removeButton, addButton and renameButton relation button
        buttonsPanel.add(removeButton);
        buttonsPanel.add(addButton);
        buttonsPanel.add(renameButton);

        // addButton buttons panel to the main RelationContainerPanel
        this.add(buttonsPanel, BorderLayout.SOUTH);

    }

    public void addRelationSelectionListeners(ListSelectionListener listener) {
        scrollableRelationList.addRelationSelectionListener(listener);
    }

    public void addRemoveButtonListener(ActionListener listener) {
        removeButton.addActionListener(listener);
    }

    public void removeSelectedRelation() {
        scrollableRelationList.removeSelected();
    }

    public void addAddRelationButtonListener(ActionListener addRelationButtonListener) {
        addButton.addActionListener(addRelationButtonListener);
    }

    public void addRelation(String s) throws RelationAlreadyInSetException {
        scrollableRelationList.addRelation(s);
    }

    /**
     * returns the currently selected option in relation
     * @return
     */
    public String getCurrentlySelected() {
        return scrollableRelationList.getCurrentlySelected();
    }

    public void renameRelation(String oldName, String newName) throws RelationNotInSetException, RelationAlreadyInSetException {
        scrollableRelationList.renameRelation(oldName, newName);
    }

    public void addRenameRelationButtonListener(ActionListener renameRelationButtonListener) {
        renameButton.addActionListener(renameRelationButtonListener);
    }

    public void setSelectedRelation(String s) {
        scrollableRelationList.setSelectedRelation(s);
    }
}
